package com.lancoo.ccas53.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.lancoo.ccas53.pojo.vo.StudentClassTree;
import com.lancoo.ccas53.pojo.vo.StudentCollegeTree;
import com.lancoo.ccas53.pojo.vo.StudentGradeTree;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 学生
 * </p>
 *
 * @author quin.song
 * @since 2024-05-20
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(of = {"studentId","taskId","baseStudentId","studentName","classId","className","majorId","majorName","baseCollegeId","collegeName","sexCode","gradeId","gradeName"})
@ApiModel(value = "Student对象", description = "学生")
public class Student implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "student_id", type = IdType.AUTO)
    private Long studentId;

    private Integer taskId;

    private String baseStudentId;

    private String studentName;

    @ApiModelProperty("学生头像")
    private String imageUrl;

    private String classId;

    private String className;

    private String majorId;

    private String majorName;

    private String baseCollegeId;

    private String collegeName;

    @ApiModelProperty("所属校区")
    private String campusId;

    private String campusName;

    @ApiModelProperty("男 女 保密")
    private String sexCode;

    private String gradeId;

    @ApiModelProperty("所属年级")
    private String gradeName;

    @ApiModelProperty("选课范围 1本专业年级  2本专业跨年级 3本院系跨年级 4全部专业年级")
    private Integer selectionScope;

    @ApiModelProperty("（预选）选课范围 1本专业年级  2本专业跨年级 3本院系跨年级 4全部专业年级")
    private Integer selectionScopePre;

    @ApiModelProperty("学分上限")
    private Double creditLimit;

    @ApiModelProperty("课程数上限")
    private Integer courseLimit;

    @ApiModelProperty("预选学分上限")
    private Double preCreditLimit;

    @ApiModelProperty("预选课程数上限")
    private Integer preCourseLimit;

    @TableField(exist = false)
    @ApiModelProperty("培养方案名称")
    private String teachingPlanName;

    public static List<StudentCollegeTree> toTree(List<Student> studentList) {
        List<StudentCollegeTree> trees = new ArrayList<>();
        for (Student student : studentList) {
            String baseCollegeId = student.getBaseCollegeId();
            String collegeName = student.getCollegeName();
            StudentCollegeTree college = null;
            for (StudentCollegeTree tree : trees) {
                if (tree.getBaseCollegeId().equals(baseCollegeId)) {
                    college = tree;
                    break;
                }
            }
            if (college == null) {
                college = new StudentCollegeTree();
                college.setBaseCollegeId(baseCollegeId);
                college.setCollegeName(collegeName);
                college.setSum(0);
                college.setGradeList(new ArrayList<>());
                trees.add(college);
            }
            String gradeId = student.getGradeId();
            String gradeName = student.getGradeName();
            StudentGradeTree gradeTree = null;
            for (StudentGradeTree studentGradeTree : college.getGradeList()) {
                if (studentGradeTree.getGradeId().equals(gradeId)) {
                    gradeTree = studentGradeTree;
                    break;
                }
            }
            if (gradeTree == null) {
                gradeTree = new StudentGradeTree();
                gradeTree.setGradeId(gradeId);
                gradeTree.setGradeName(gradeName);
                gradeTree.setSum(0);
                gradeTree.setClassList(new ArrayList<>());
                college.getGradeList().add(gradeTree);
            }
            String classId = student.getClassId();
            String className = student.getClassName();
            StudentClassTree classTree = null;
            for (StudentClassTree studentClassTree : gradeTree.getClassList()) {
                if (studentClassTree.getClassId().equals(classId)) {
                    classTree = studentClassTree;
                    break;
                }
            }
            if (classTree == null) {
                classTree = new StudentClassTree();
                classTree.setClassId(classId);
                classTree.setClassName(className);
                classTree.setSum(0);
                classTree.setStudentList(new ArrayList<>());
                gradeTree.getClassList().add(classTree);
            }
            classTree.getStudentList().add(student);
            classTree.setSum(classTree.getSum() + 1);
            gradeTree.setSum(gradeTree.getSum() + 1);
            college.setSum(college.getSum() + 1);
        }
        trees.forEach(t -> {
            t.setGradeList(t.getGradeList().stream().sorted(Comparator.comparing(StudentGradeTree::getGradeName)).collect(Collectors.toList()));
            t.getGradeList().forEach(g -> {
                g.setClassList(g.getClassList().stream().sorted(Comparator.comparing(StudentClassTree::getClassName)).collect(Collectors.toList()));

            });
        });
        return trees;
    }
}
